<?php
class import_salary_m extends MY_Model{
	
	function __construct(){
		parent::__construct();
	}
	function getid_room($id_user)
	{
		$id_room = "";
		$this->db->where("id",$id_user);
		$id = $this->db->get("emp_profile")->row();
		if(!empty($id))
			$id_room = $id->room_id;
		return $id_room;
	}
	function name_room($id_room,$i)
	{
		if($i == 0)
			$data["title"] = "File tổng hợp";
		else
		{
			$symbol = substr($id_room,0,1);
			$num 	= substr($id_room,1,1);
			if($symbol == "_")
			{
				$this->db->select("room.*");
				$this->db->from("emp_room room");						
				$this->db->where("id",$num);
				$data = $this->db->get()->row_array();
			}
			else
			{
				$this->db->select("room.*");
				$this->db->from("emp_room room");	
				$this->db->where("room.id",$id_room);
				$data = $this->db->get()->row_array();
				if(!empty($data))	
					$data["title"] = "Tổng hợp phòng ".$data["title"];
			}
		}
		return (!empty($data["title"]))?$data["title"]:"File tổng hợp";
	}
	function list_em_room()
	{
		$this->db->from("emp_room");
		$this->db->where_not_in("parent_id",array(0));		
		$list_room = $this->db->get()->result_array();
		$list_room = $this->list_em($list_room);
		if(!empty($list_room))
		{
			$i = 0;
			foreach($list_room as $item)
			{
				$list_em = $this->get_room_parent($item["id"]);	
				if(!empty($list_em)){
					$list_room[$i]["employ"] = array_merge($list_room[$i]["employ"],$list_em);
				}
				$i++;
			}
		}
		return $list_room;
	}
	function get_room_parent($parent_id="",$list_em="")
	{
		if($parent_id != "")
		{
			$this->db->where("parent_id",$parent_id);
			$data = $this->db->get("emp_room")->result_array();
		}
		if(!empty($data))foreach($data as $item)
		{
			if($list_em == "")
				$list_em = $this->list_em_parent($item["id"]);
			else
				$list_em = array_merge($list_em,$this->list_em_parent($item["id"]));
			$this->get_room_parent($item["id"],$list_em);	
		}
		return $list_em;
	}
	function list_em_parent($id)
	{
		$this->db->where("emp_profile.room_id",$id);
		$this->db->join("ser_users","ser_users.id = emp_profile.id");
		return $this->db->get("emp_profile")->result_array();	
		
	}
	function list_em($list_room)
	{
		if(!empty($list_room))
		{
			$count = 0;
			foreach($list_room as $item)
			{
				$this->db->where("emp_profile.room_id",$item["id"]);
				$this->db->join("ser_users","ser_users.id = emp_profile.id");
				$list_room[$count++]["employ"] =$this->db->get("emp_profile")->result_array();	
			}	
		}
		return $list_room;
	}
	function list_sabbatical($year,$month,$day,$id)
	{
		$this->db->from("em_off_".$year);
		$this->db->where("month",$month);
		$this->db->where("time_start",$day);
		$this->db->where("user_id",$id);
		$this->db->where("status",1);
		$data = $this->db->get()->row_array();
		if(!empty($data))
		{
			if($data["important"] == 1 and $data["boss_status"] == 0)
				$data = "";
		}	
		if(empty($data))// kiem tra ngay do co nam giua ngay ngay bat dau va ket thuc khong
		{
			$data = $this->list_sabbatical_theongay($year,$month,$day,$id);	
		}
		return $data;
	}
	function list_sabbatical_theongay($year,$month,$day,$id)
	{
		$this->db->from("em_off_".$year);
		$this->db->where("month",$month);
		$this->db->where("time_start <= ",$day+86399);
		$this->db->where("time_end > ",$day);
		$this->db->where("user_id",$id);
		$this->db->where("status",1);
		$data = $this->db->get()->row_array();
		if(!empty($data))
		{
			if($data["important"] == 1 and $data["boss_status"] == 0)
				$data = "";
		}	
		return $data;
	}
	function update_salary($id_room,$id_em,$month,$year,$data)
	{
		$this->db->where("room_id",$id_room);
		$this->db->where("user_id",$id_em);
		$this->db->where("month",$month);
		return $this->db->update("emp_salary_".$year,$data);	
	}
	function update_date($year,$month,$data)
	{
		$this->db->where("year",$year);
		$this->db->where("month",$month);
		return $this->db->update("emp_day",$data);	
	}
	function get_id_form_id2($id2){
		$id2 = $this->db->select("id")->from("emp_profile")->where("id2",$id2)->get()->row();
		$id = (!empty($id2->id))?$id2->id:"";
		return $id;
	}
	function update_deduction($year,$month,$id2,$type,$data){
		$id = $this->get_id_form_id2($id2);
		if(!empty($id))
		{
			$this->update_PCCN($year,$month,$type);
			$this->db->where("month",$month);
			$this->db->where("user_id",$id);
			return $this->db->update("emp_salary_".$year,$data);				
		}
		return 0;
	}
	function update_PCCN($year,$month,$type)
	{
		$this->db->where("month",$month);
		$data = "";
		if($type == 0)
			$data = array("salary_add5"=>0);
		elseif($type == 1)
			$data = array("except4"=>0,"except5"=>0);
		if(!empty($data))
			$this->db->update("emp_salary_".$year,$data);
		return ;	
	}
}